

// pages/materials/materials.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        materials: [],
        selectedItems: {},
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getAllMaterials();
    },
    getMaterial(id) {
        for (let index in this.data.materials) {
            let m = this.data.materials[index]
            if (m.id == id)
                return m;
        }
    },
    onCollectCountChange(e) {
        let id = e.currentTarget.dataset.id;
        let value = e.detail.value;
        let selectedItems = this.data.selectedItems;
        selectedItems[id] = parseInt(value);
        if (value > this.getMaterial(id).max_count) {
            value = this.getMaterial(id).max_count;
        }
        if (parseInt(value) > 0) {

            this.setData({
                selectedItems
            })
        } else {
            delete selectedItems[id];
            value = "";
        }
        return value
        console.log(id, value)
    },

    getAllMaterials() {
        let storeId = this.options.storeId
        let that = this;
        wx.scommon.rget(wx.surls.material.getCanCollectList.add({
            storeId: storeId
        }), function (res) {
            that.setData({
                materials: res.data
            })
        }, function () {
            that.setData({
                healthCode: -2
            })
        })
    },
    decreateOneCount(e) {
        let id = e.currentTarget.dataset.id;
        this.changeCount(-1, id)
    },
    addOneCount(e) {
        let id = e.currentTarget.dataset.id;
        this.changeCount(1, id)
    },
    changeCount(value, id) {
        console.log(value, id)
        // debugger
        let selectedItems = this.data.selectedItems;
        if (selectedItems[id] == null) {
            if (value < 0) {
                return;
            } else {
                selectedItems[id] = 0
            }

        }
        let newValue = selectedItems[id] + value;
        let max_count = this.getMaterial(id).max_count;
        let store_count = this.getMaterial(id).current_count;
        if (newValue > max_count) {
            newValue = max_count
        }
        if (newValue > store_count) {
            newValue = store_count
        }
        if (newValue <= 0) {
            delete selectedItems[id];
        } else {
            selectedItems[id] = newValue
        }
        this.setData({
            selectedItems
        })
    },
    createCollectionOrder() {
        let selectedItems = this.data.selectedItems;
        if (Object.keys(selectedItems).length <= 0) {
            wx.su.toast("请选择物品")
            return;
        }
        let items = [];
        for (let key in this.data.selectedItems) {
            items.push({
                material_id: key,
                schedule_count: this.data.selectedItems[key]
            })
        }
        let requestBody = {
            user_id: getApp().userinfo.id,
            name: "物资领用单",
            items: items,
            storeId:this.options.storeId
        }
        wx.showModal({
            title: '提示',
            content: '确认提交？',
            complete: (res) => {
                if (res.cancel) {

                }

                if (res.confirm) {
                    wx.scommon.rpost(wx.surls.collectionOrder.createOrder, requestBody, function (res) {
                        wx.navigateBack();
                            
                    }, function () {
                        wx.su.toast("提交失败")
                    })
                }
            }
        })

    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})